package xyz.wanwanwan.www.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.wanwanwan.www.hrm.constant.CodeConstant;
import xyz.wanwanwan.www.hrm.constant.RegisterChannelConstant;
import xyz.wanwanwan.www.hrm.domain.Sso;
import xyz.wanwanwan.www.hrm.domain.VipBase;
import xyz.wanwanwan.www.hrm.dto.SMSCodeDto;
import xyz.wanwanwan.www.hrm.feign.client.CacheFeign;
import xyz.wanwanwan.www.hrm.mapper.SsoMapper;
import xyz.wanwanwan.www.hrm.mapper.VipBaseMapper;
import xyz.wanwanwan.www.hrm.service.ISsoService;
import xyz.wanwanwan.www.hrm.util.AjaxResult;
import xyz.wanwanwan.www.hrm.util.MD5Utils;
import xyz.wanwanwan.www.hrm.util.StrUtils;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author Mr.Fu
 * @since 2020-09-17
 */
@Service
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {
    @Autowired
    private CacheFeign cacheFeign;
    @Autowired
    private SsoMapper ssoMapper;
    @Autowired
    private VipBaseMapper vipBaseMapper;
    /**
     * 注册用户
     * @param smsCodeDto
     */
    @Override
    public void register(SMSCodeDto smsCodeDto) {
        //数据验证
        checkParam(smsCodeDto);
        //数据转换
        Sso sso = dtoToSso(smsCodeDto);
        //将数据存入数据库
        ssoMapper.insert(sso);
        //将数据保存到基础表
        VipBase vipBase = dtoToVipBase(sso);
        vipBaseMapper.insert(vipBase);
    }
    /**
     * 验证数据
     * @param smsCodeDto
     */
    public void checkParam(SMSCodeDto smsCodeDto){
        AjaxResult fromRedis = cacheFeign.getFromRedis(CodeConstant.PHONE_CODE_KEY+":"+smsCodeDto.getMobile());
        if(fromRedis.isSuccess()){
            String resultObj = fromRedis.getResultObj().toString();
            if(StringUtils.isEmpty(smsCodeDto.getMobile())
                    &&StringUtils.isEmpty(smsCodeDto.getPassword())
                    &&StringUtils.isEmpty(resultObj)
                    &&StringUtils.isEmpty(smsCodeDto.getImageCode())){
                throw new RuntimeException("数据不能为空");
            }
            Sso sso = ssoMapper.selectByPhone(smsCodeDto.getMobile());
            if(sso!=null){
                throw new RuntimeException("您已被注册过!!!!!!!");
            }
            String mobileCode = resultObj.split(",")[0];
            if(!smsCodeDto.getSmsCode().toUpperCase().equals(mobileCode.toUpperCase())){
                throw new RuntimeException("短信验证码错误");
            }
        }
    }
    /**
     * 数据转换
     * @param smsCodeDto
     * @return
     */
    public Sso dtoToSso(SMSCodeDto smsCodeDto){
        Sso sso = new Sso();
        //将用户电话存入sso
        sso.setPhone(smsCodeDto.getMobile());
        //设置盐值
        String salt = StrUtils.getComplexRandomString(10);
        //获取加密密码
        String password = MD5Utils.encrypByMd5(smsCodeDto.getPassword() + salt);
        sso.setSalt(salt);
        sso.setPassword(password);
        sso.setCreateTime(System.currentTimeMillis());
        return sso;
    }

    /**
     * 保存基础表
     * @param sso
     * @return
     */
    public VipBase dtoToVipBase(Sso sso){
        VipBase vipBase = new VipBase();
        Long time = sso.getCreateTime();
        vipBase.setCreateTime(time);
        vipBase.setRegTime(time);
        vipBase.setRegChannel(RegisterChannelConstant.REGISTER_CHANNEL_SMS);
        vipBase.setSsoId(sso.getId());
        return vipBase;
    }
}
